﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;

public partial class root_admin_deleteAttributeValue : OOSPage
{
    PathInfo _pathInfo;

    class PathInfo
    {
        [PathInfoMember(0)]
        internal long categoryID;

        [PathInfoMember(1)]
        internal long attributeID;

        [PathInfoMember(2)]
        internal short attributeValue;

        [PathInfoMember(3)]
        internal int deleteType;
    }

    protected void Page_Init(object sender, EventArgs e)
    {
#warning admin
        //OOSRequest.Current.EnsureModerator();
        _pathInfo = EnablePathInfo<PathInfo>();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string categoryTitle = "category";
        string attributeTitle = "attribute";
        string attributeValueTitle = "attribute value";
        string question = "Do you really want to delete this {0}?<br/><br/>";
        string title = "Delete {0}";

        switch (_pathInfo.deleteType)
        {
            case (int)Helper.Entity.Category:
                PageTitle = String.Format(title, categoryTitle);
                OctoOS.UI.CheckBox chb = new OctoOS.UI.CheckBox("CheckSure", " Deleting of category leads to deleting of all ads in this category");
                deleteMsgBox.SetContent(new System.Web.UI.WebControls.Literal { Text = String.Format(question, categoryTitle) }, chb);
                break;

            case (int)Helper.Entity.Attribute:
                PageTitle = String.Format(title, attributeTitle);
                OctoOS.UI.CheckBox chbAtt = new OctoOS.UI.CheckBox("CheckSure", " Deleting of attribute may affect ads containing this attribute");
                deleteMsgBox.SetContent(new System.Web.UI.WebControls.Literal { Text = String.Format(question, attributeTitle) }, chbAtt);
                break;

            case (int)Helper.Entity.AttributeValue:
                PageTitle = String.Format(title, attributeValueTitle);
                deleteMsgBox.SetContent(String.Format(question, attributeValueTitle));
                break;
        }
        Submit yesBtn = deleteMsgBox.AddSubmit("yesBtn", "Yes");
        yesBtn.Click += new EventHandler(yesBtn_Click);
        Submit noSubmit = deleteMsgBox.AddSubmit("noSubmit", "No");
        noSubmit.Click += new EventHandler(noSubmit_Click);
    }

    void noSubmit_Click(object sender, EventArgs e)
    {
        switch (_pathInfo.deleteType)
        {
            case (int)Helper.Entity.AttributeValue:
                Response.Redirect(Urls.Classifieds.GetAttribute(_pathInfo.categoryID, _pathInfo.attributeID));
                break;
            case (int)Helper.Entity.Attribute:
                Response.Redirect(Urls.Classifieds.GetCategoryEdit(_pathInfo.categoryID));
                break;
            case (int)Helper.Entity.Category:
                Response.Redirect(Urls.Classifieds.GetCategoryEdit(_pathInfo.categoryID));
                break;

        }
    }

    void yesBtn_Click(object sender, EventArgs e)
    {
        switch (_pathInfo.deleteType)
        {
            case (int)Helper.Entity.AttributeValue:
                DataSource.ClassifiedAttributeValues_Delete(
                    _pathInfo.categoryID,
                    _pathInfo.attributeID,
                    _pathInfo.attributeValue);
                Response.Redirect(Urls.Classifieds.GetAttribute(_pathInfo.categoryID, _pathInfo.attributeID));
                break;
            case (int)Helper.Entity.Attribute:
                if (((OctoOS.UI.CheckBox)deleteMsgBox.FindControl("CheckSure")).Checked)
                {
                    DataSource.ClassifiedAttributes_Delete(
                        _pathInfo.categoryID,
                        _pathInfo.attributeID);
                    Response.Redirect(Urls.Classifieds.GetCategoryEdit(_pathInfo.categoryID));
                }
                else
                {
                    warning.Visible = true;
                }
                break;
            case (int)Helper.Entity.Category:
                var chb = (deleteMsgBox.FindControl("CheckSure") as OctoOS.UI.CheckBox);
                if (chb != null)
                {
                    if (chb.Checked)
                    {
                        DataSource.ClassifiedCategories_Delete(
                            OOSRequest.Current.ResourceID,
                            _pathInfo.categoryID);
                        Response.Redirect(Urls.Classifieds.GetAdminDefault());
                    }
                    else
                    {
                        warning.Visible = true;
                    }
                }
                
                break;
        }

    }

    protected void CheckValidate(object sender, ValidateEventArgs e)
    {
        if (!((OctoOS.UI.CheckBox)sender).Checked)
        {
            e.ErrorMessage = "";
        }
    }
}